using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.Configuration;
using System.Configuration;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Administration; //for the web application stuff
using Microsoft.SharePoint;

namespace IWCommunity.Common
{
   public static class  Utilities
    {
            /// <summary>
            /// html table for error messages in SharePoint
            /// </summary>
            /// <param name="ErrorMessage"></param>
            /// <returns></returns>
            public static Table CreateErrorMessage(string ErrorMessage)
            {
                Table tbl = new Table();
                TableRow tr = new TableRow();
                TableCell td = new TableCell();

                td.Text = ErrorMessage;

                tr.Cells.Add(td);
                tbl.Rows.Add(tr);
                tbl.CssClass = "ms-WPBody";

                return tbl;
            }

           /// <summary>
           /// Returns membership provider for the current SharePoint web application. This allows it to be different
           /// to web.config settings as you can set the provider through the MOSS central admin
           /// </summary>
           /// <param name="context"></param>
           /// <returns></returns>
           public static string GetMembershipProvider(HttpContext context)
           {
               SPWebApplication app = SPWebApplication.Lookup(new Uri(context.Request.Url.AbsoluteUri));
               SPIisSettings settings = app.IisSettings[SPUrlZone.Default];
               return settings.MembershipProvider;
           }

       public static string GetDefaultMembershipProviderConnectionString()
       {
           MembershipSection configSection = (MembershipSection)ConfigurationManager.GetSection("system.web/membership");
           string conName = configSection.Providers[configSection.DefaultProvider].Parameters["connectionStringName"].ToString();
           return ConfigurationManager.ConnectionStrings[conName].ConnectionString;

       }

           public static string GetMembershipProvider(SPSite site)
           {
               // get membership provider of whichever zone in the web app is fba enabled 
               SPIisSettings settings = GetFBAIisSettings(site);
               return settings.MembershipProvider;
           }

           public static SPIisSettings GetFBAIisSettings(SPSite site)
           {
               SPIisSettings settings = null;

               // try and get FBA IIS settings from current site zone
               try
               {
                   settings = site.WebApplication.IisSettings[site.Zone];
                   if (settings.AuthenticationMode == AuthenticationMode.Forms)
                       return settings;
               }
               catch
               {
                   // expecting errors here so do nothing                 
               }

               // check each zone type for an FBA enabled IIS site
               foreach (SPUrlZone zone in Enum.GetValues(typeof(SPUrlZone)))
               {
                   try
                   {
                       settings = site.WebApplication.IisSettings[(SPUrlZone)zone];
                       if (settings.AuthenticationMode == AuthenticationMode.Forms)
                           return settings;
                   }
                   catch
                   {
                       // expecting errors here so do nothing                 
                   }
               }

               // return null if FBA not enabled
               return null;
           }

           /// <summary>
           /// check current site to see if a provider has been specified in the web.config
           /// </summary>
           /// <returns></returns>
           public static bool IsProviderConfigured()
           {
               // attempt to get current users details
               try
               {
                   Membership.GetUser();
               }
               catch
               {
                   // if fails membership provider is not configured correctly
                   return false;
               }

               // if no error provider is ok
               return true;
           }

           public static bool IsWebAppFBAEnabled(SPSite site)
           {
               SPIisSettings settings = site.WebApplication.IisSettings[site.Zone];
               return (settings.AuthenticationMode == AuthenticationMode.Forms);
           }
    }
}
